package com.framework.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartException;

import com.framework.bdt.FileBdt;
import com.framework.bdt.enumClass.FileType;

public abstract class AbstractFileServerUtility {

	@Value("${tempFolder}")
	private static String tempFolder;
	@Value("${fileUploadUrl}")
	private static String fileUploadUrl;
	@Value("${fileDownloadUrl}")
	private static String fileDownloadUrl;

	/**
	 * File Upload
	 *
	 * @param fileType
	 * @param fileBdt
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	protected static boolean runFileServerUpload(FileType fileType, FileBdt fileBdt) throws IOException {
		List<File> destination = new ArrayList<File>();
		try {
			if (null == fileBdt)
				throw new MultipartException("File Not Found");
			String fileSeparator = System.getProperty("file.separator");
			if (!fileBdt.isEmpty()) {
				destination.add(new File(new File("").getAbsoluteFile() + fileSeparator + fileBdt.getOriginalFileNameExtension()));
			}
			sendFileUpolad(fileType, fileBdt.getFolderName(), destination);
		} finally {
			for (File file : destination) {
				file.delete();
			}
			destination = null;
		}
		return false;
	}

	/**
	 * Multi File Server Upload
	 *
	 * @param fileType
	 * @param fileBdts
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	protected boolean runFileServerUpload(FileType fileType, List<FileBdt> fileBdts) throws IOException {
		if (null == fileBdts)
			throw new MultipartException("File Not Found");
		List<File> destination = new ArrayList<File>();
		String folderName = null;
		try {
			for (FileBdt fileBdt : fileBdts) {
				if(null == folderName && "".equals(fileBdt.getFolderName())){
					folderName = fileBdt.getFolderName();
				}
				if (!fileBdt.isEmpty()) {
					destination.add(fileBdt.getFile());
				}
			}
			sendFileUpolad(fileType, folderName, destination);
		} finally {
			for (File file : destination) {
				file.delete();
			}
			destination = null;
		}
		return false;
	}

	private static boolean sendFileUpolad(FileType fileType, String folder, List<File> files) throws IOException {
		HttpClient client = new HttpClient();
		PostMethod mPost = new PostMethod(fileUploadUrl);
		mPost.setParameter("folderName", folder);
		mPost.setParameter("fileType", fileType.toString());
		Part[] parts = new Part[files.size()];
		int i = 0;
		for (File file : files) {
			parts[i] = new FilePart(file.getName(), file);
			i++;
		}
		mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost.getParams()));
		try {
			int status = client.executeMethod(mPost);
			if (HttpStatus.SC_OK == status) {
				return true;
			} else {
				throw new HttpException("Status Fail / Status state : " + status);
			}
		} finally {
			mPost.releaseConnection();
		}
	}
}
